// 信息发布-->电子公告查询
Ext.onReady(function(){
    Ext.define('json_data',{
        extend: 'Ext.data.Model',
        fields: [
			 {name:'id',type:'string'},
             {name:'employe_id',type:'string'},
             {name:'employe__name',type:'string'},
             {name:'title',type:'string'},
             {name:'valid_start',type:'date'},
             {name:'valid_end',type:'date'},
             {name:'is_top',type:'string'},
             {name:'content',type:'string'}
        ]
    });
    //创建数据源
    var store = Ext.create('Ext.data.Store', {
    	pageSize:20,
        model: 'json_data',
	    proxy: {
	        type: 'ajax',
	        url: '/resourcecontrol/notice_list/',
	        actionMethods:'post',
	        reader: {
	            type: 'json',
	            root: 'rows',
	            totalProperty: 'totalCount'
	        }
	    },
	    autoLoad: true
    });
    //多选
    var selModel = Ext.create('Ext.selection.CheckboxModel');
    //创建Grid
    var grid = Ext.create('Ext.grid.Panel',{
        store: store,
        //height: 500,
        //selModel:selModel,
        columnLines:true,
        columns: [
            Ext.create('Ext.grid.RowNumberer'),// 序列
			{text: "ID", width: 80, dataIndex: 'id', sortable: false,hidden : true},
            {text: "发布人ID", width: 80, dataIndex: 'employe_id', sortable: false,hidden : true},
            {text: "发布人", width: 80, dataIndex: 'employe__name', sortable: false},
            {text: "标题", width: 180, dataIndex: 'title', sortable: false},
            {text: "生效时间", width: 160, dataIndex: 'valid_start', sortable: false,renderer: Ext.util.Format.dateRenderer('Y-m-d'),hidden : true},
            {text: "失效时间", width: 160, dataIndex: 'valid_end', sortable: false,renderer: Ext.util.Format.dateRenderer('Y-m-d'),hidden : true},
            {text: "是否置顶", width: 60, dataIndex: 'is_top', sortable: false,
            	renderer:function(v){
            	if (v==1) 
                	return '是'  
    			else
        			return '否'
            	},hidden : true},
            {text: "正文", width: 550, dataIndex: 'content', sortable: false}
        ],
        // disableSelection: false,
        layout : 'fit',
        //frame:true,
        loadMask:true,
        region: 'north',
        //renderTo: 'query_notice-grid',
        viewConfig: {
            stripeRows: true,
            listeners: {
                itemclick: function(dataview, record, item, index, e) {
    				
                },
                itemdblclick: function(dataview, record, item, index, e) {
                	var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
	        			var selected = grid.getSelectionModel().getSelection();
						form.loadRecord(selected[0]);
	        			win.show();
        			}else{
        				Ext.Msg.alert('系统提示', '请选择需要修改的公告!');
        			}
                }
            }
        },
        dockedItems : [{
            xtype : 'pagingtoolbar',
            store : store,
            dock : 'bottom',
            displayInfo : true,
            //displayMsg: '显示 {0} - {1} 条，共计 {2} 条',
            autoScroll:true 
        },{
        	dock:'top',
        	xtype:'toolbar',
        	items:[{
        		id:'title',
        		xtype:'textfield',
        		labelWidth: 50,
        		fieldLabel:'标题'
        	},{
        		xtype:'button',
        		text:'查询',
        		iconCls:'search', 
        		handler:function(){
        			var params = { title: Ext.getCmp('title').getValue() };
        		    Ext.apply(store.proxy.extraParams, params);
        		    store.currentPage=1;
        		    store.load();
        		}
        	},{
        		xtype:'button',
        		text:'详情',
        		iconCls:'edit', 
        		handler:function(){
        			var selected = grid.getSelectionModel().getSelection();
        			if(selected!=''){
	        			form.getForm().reset();
	        			var selected = grid.getSelectionModel().getSelection();
						form.loadRecord(selected[0]);
	        			win.show();
	        		}else{
        				Ext.Msg.alert('系统提示', '请选择需要查看详情的公告!');
        			}
        		}
        	}]
        }]
    });
    

	var form = Ext.create('Ext.form.Panel', {
		//title: '电子公告信息',
		width: 550,
		//frame: true,
		bodyPadding: 5,
        fieldDefaults: {
            labelAlign: 'right',
            labelWidth: 90,
            msgTarget: 'side'
        },
        layout: 'anchor',
        defaults: {
            anchor: '100%'
        },
		items : [{
				xtype:'hiddenfield',
				fieldLabel:'id',
				name:'id'
			},{
				xtype:'hiddenfield',
				fieldLabel:'员工',
				allowBlank : false,
				name:'employe_id'
			},{
				xtype:'textfield',
				fieldLabel:'标题',
				allowBlank : false,
				name:'title'
			},{
				xtype:'datefield',
				fieldLabel:'生效时间',
				allowBlank : false,
				format: 'Y-m-d',
				name:'valid_start'
			},{
				xtype:'datefield',
				fieldLabel:'失效时间',
				allowBlank : false,
				format: 'Y-m-d',
				name:'valid_end'
			},{
				xtype:'textfield',
				fieldLabel:'是否置顶',
				allowBlank : false,
				name:'is_top'
			},{
				xtype:'textareafield',
				fieldLabel:'正文',
				allowBlank : false,
				name:'content'
		}]
	});

	var win = Ext.create('Ext.window.Window', {
		//title : '---',
		modal: true,
		closeAction: 'hide',
		items : [form],
		plain : true,
		resizable : false,
		buttonAlign : 'center',
		buttons : [{
			text : '确定',
			handler : function() {
				form.getForm().reset();
				win.hide();
			}
		} ]
	});    
    
	Ext.create('Ext.container.Viewport',{
		layout:'fit',
		renderTo: 'query_notice-grid',
		items:[grid]
	});
	
});